<script lang="ts" setup>
import { FormTypeEnum } from "@/common/constants/other-keys"
import { getProjectId, getProjectModules, getProjectName } from "@/common/utils/cache/local-storage"
import CreateOrUpdate from "@/pages/dashboard/components/create-or-update.vue"
import { useAppStore } from "@/pinia/stores/app"
import { useSettingsStore } from "@/pinia/stores/settings"
import { useUserStore } from "@/pinia/stores/user"
import { ProjectRoutes, RouteTitleMap } from "@/router"
import Notify from "@@/components/Notify/index.vue"
import Screenfull from "@@/components/Screenfull/index.vue"
import SearchMenu from "@@/components/SearchMenu/index.vue"
import ThemeSwitch from "@@/components/ThemeSwitch/index.vue"
import { useDevice } from "@@/composables/useDevice"
import { useLayoutMode } from "@@/composables/useLayoutMode"
import { UserFilled } from "@element-plus/icons-vue"
import { computed } from "vue"
import { isEmpty } from "xe-utils"
import { Hamburger, Sidebar } from "../index"
import { useTagsViewStore } from "@/pinia/stores/tags-view"
import { useRouteListener } from "@/common/composables/useRouteListener"
import { usePermissionStore } from "@/pinia/stores/permission"

const { listenerRouteChange } = useRouteListener()
const { isMobile } = useDevice()
const { isTop } = useLayoutMode()
const router = useRouter()
const appStore = useAppStore()
const userStore = useUserStore()
const settingsStore = useSettingsStore()
const { showNotify, showThemeSwitch, showScreenfull, showSearchMenu } = storeToRefs(settingsStore)
const dialogVisible = ref<boolean>(false)
const curId = ref<string>()
const tagsViewStore = useTagsViewStore()
const permissionStore = usePermissionStore()
const projectTitle = ref()

/** 切换侧边栏 */
function toggleSidebar() {
  appStore.toggleSidebar(false)
}

/** 登出 */
function logout() {
  userStore.logout()
  router.replace("/login")
}

/** 返回主页 */
function handleBack() {
  router.replace('/dashboard')
  tagsViewStore.delAllViews()
  // const fullPath = `http://${new URL("/", import.meta.url).host}/`
  // location.href = fullPath
}

const isChild = computed(() => !isEmpty(permissionStore.childFlag))

const isProject = computed(() => {
  return permissionStore.childFlag == "MyProject"
})

async function setProjectTitle() {
  if (permissionStore.childFlag == undefined) {
    projectTitle.value = undefined
  } else if (permissionStore.childFlag == "MyProject") {
    // 根据id查询项目名称
    projectTitle.value = getProjectName()
  } else {
    projectTitle.value = RouteTitleMap[permissionStore.childFlag]
  }
}

function handleView() {
  curId.value = getProjectId()
  dialogVisible.value = true
}

function closeDialog(type?: string) {
  dialogVisible.value = false
  const modules = getProjectModules()
  permissionStore.setShowMenus()
  for (const item of ProjectRoutes[0].children || []) {
    if (item.meta && modules.includes(item.meta.moduleName as string)) {
      router.replace({ path: item.path })
      tagsViewStore.delAllViews()
      break
    }
  }
}

watch(
  () => isChild.value,
  () => {
    setProjectTitle()
  }
)

onMounted(() => {
  setProjectTitle()
})
</script>

<template>
  <div class="navigation-bar">
    <div v-if="projectTitle !== undefined" class="back-btn hamburger">
      <el-tooltip
        class="box-item"
        effect="dark"
        content="返回"
        placement="bottom-end"
      >
        <el-icon :size="26" @click="handleBack">
          <ArrowLeft />
        </el-icon>
      </el-tooltip>
    </div>
    <Hamburger
      v-if="!projectTitle && (!isTop || isMobile)"
      :is-active="appStore.sidebar.opened"
      class="hamburger"
      @toggle-click="toggleSidebar"
    />
    <div />
    <div v-if="projectTitle !== undefined" class="project-name" :class="{ 'is-top': isTop }">
      <el-space alignment="center">
        {{ projectTitle }}
        <el-tooltip v-if="isProject" effect="dark" content="项目信息" placement="bottom">
          <SvgIcon name="info" class="project-info-btn" @click="handleView" />
        </el-tooltip>
      </el-space>
    </div>
    <Sidebar v-if="isTop && !isMobile" class="sidebar" />
    <div class="right-menu">
      <SearchMenu v-if="showSearchMenu" class="right-menu-item" />
      <Screenfull v-if="showScreenfull" class="right-menu-item" />
      <ThemeSwitch v-if="showThemeSwitch" class="right-menu-item" />
      <Notify v-if="showNotify" class="right-menu-item" />
      <el-dropdown>
        <div class="right-menu-item user">
          <div class="user-avatar">
            <img :src="`/model-api${userStore.avatar}`" alt="">
          </div>
          <span>{{ userStore.username }}</span>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="router.replace('/info')">
              个人中心
            </el-dropdown-item>
            <el-dropdown-item divided @click="logout">
              退出登录
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>

    <CreateOrUpdate
      ref="editDialogRef" :id="curId" :dialog-type="FormTypeEnum.VIEW" :visible="dialogVisible"
      @close-dialog="closeDialog"
    />
  </div>
</template>

<style lang="scss" scoped>
.project-info-btn {
  cursor: pointer;
  fill: #99a3c1 !important;
}
.user-avatar {
  width: 30px;
  height: 30px;
  border: 1px solid #ccc;
  background-color: #ccc;
  border-radius: 50%;
  margin-right: 10px;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
}
.navigation-bar {
  height: var(--v3-navigationbar-height);
  overflow: hidden;
  color: var(--v3-navigationbar-text-color);
  display: flex;
  justify-content: space-between;
  .hamburger {
    display: flex;
    align-items: center;
    height: 100%;
    cursor: pointer;
  }
  .back-btn {
    display: flex;
    align-items: center;
    height: 100%;
    cursor: pointer;
    .el-icon {
      padding: 5px;
      &:hover {
        background-color: rgba(255, 255, 255, 0.08);
      }
    }
  }
  .project-name {
    font-size: 18px;
    flex: 1;
    display: flex;
    align-items: center;
    &.is-top {
      flex: none;
    }
  }
  .breadcrumb {
    flex: 1;
    // 参考 Bootstrap 的响应式设计将宽度设置为 576
    @media screen and (max-width: 576px) {
      display: none;
    }
  }
  .sidebar {
    flex: 1;
    // 设置 min-width 是为了让 Sidebar 里的 el-menu 宽度自适应
    min-width: 0px;
    :deep(.el-menu) {
      background-color: transparent;
    }
    :deep(.el-sub-menu) {
      &.is-active {
        .el-sub-menu__title {
          color: var(--el-color-primary);
        }
      }
    }
  }
  .right-menu {
    margin-right: 10px;
    height: 100%;
    display: flex;
    align-items: center;
    &-item {
      margin: 0 10px;
      cursor: pointer;
      &:last-child {
        margin-left: 20px;
      }
    }
    .user {
      display: flex;
      align-items: center;
      .el-avatar {
        margin-right: 10px;
      }
      span {
        font-size: 16px;
      }
    }
  }
}
.hamburger + .hamburger {
  padding-right: 15px;
}
.hamburger:first-child {
  padding-left: 15px;
  padding-right: 15px;
}
</style>
